package de.uniba.wiai.lspi.chord.service.impl;

import de.uniba.wiai.lspi.chord.com.CommunicationException;
import de.uniba.wiai.lspi.chord.com.Node;

public class CheckFusionNodeTask implements Runnable {

	/**
	 * Reference on routing table.
	 */
	private References references;
	
	private Node localNode = null;
	private Object mutex = new Object();
	private boolean isTrying = false;
	
	CheckFusionNodeTask(References references, Node localNode) {
		if (references == null || localNode == null) {
			throw new NullPointerException(
					"Parameter references or Local Node may not be null!");
		}

		this.references = references;
		this.localNode = localNode;
	}
	
	@Override
	public void run() {
		Node FusionNode = this.references.getFusionNode();
		if (FusionNode != null) {
			try {
				FusionNode.ping();
				return;
			} catch (CommunicationException e) {
				this.references.removeFusionNode();
				// try to connect to another fusion group
				this.localNode.joinFusionGroup();
			}
		} else {
			// try to connect to another fusion group
			this.localNode.joinFusionGroup();
		}
	}

}
